package com.turbo.boot.crawler.spider.worker.impl.quota;

import com.alibaba.fastjson.JSON;
import com.turbo.boot.crawler.dao.dal.CrawlerResultDal;
import com.turbo.boot.crawler.dao.entity.CrawlerResultEntity;
import com.turbo.boot.crawler.spider.worker.BaseTurboCrawlerPageParser;
import com.xuxueli.crawler.annotation.PageFieldSelect;
import com.xuxueli.crawler.annotation.PageSelect;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 描述
 *
 * @author wuyn
 * @since 2024/4/19
 */
@Service("sinaHqUsdInrCrawlerPageParser")
public class SinaHqUsdInrCrawlerPageParser extends BaseTurboCrawlerPageParser<SinaHqUsdInrCrawlerPageParser.PageVo> {

    @Resource
    private CrawlerResultDal crawlerResultDal;


    @Override
    public void parse(Document html, Element pageVoElement, PageVo pageVo) {
        // 解析封装 PageVo 对象
        String pageUrl = html.baseUri();
        if (StringUtils.isEmpty(pageVo.getCode())) {
            return;
        }
        System.out.println(pageUrl + "：" + pageVo.toString());
        // 处理业务代码

        CrawlerResultEntity entity = buildCrawlerResultEntity();
        entity.setQuotaCode(pageVo.getCode());
        entity.setQuotaName(pageVo.getName());
        entity.setQuotaValue(pageVo.getPrice());
        entity.setCollectedAt(new Date());

        crawlerResultDal.insert(entity);
    }


    @Setter
    @Getter
    @PageSelect(cssQuery = "#quoteWrap")
    public static class PageVo {

        @PageFieldSelect(cssQuery = ".price")
        private String price;

        private String name = "美元印度卢比";
        private String code = "USDINR";

        private Date time = new Date();


        @Override
        public String toString() {
            return JSON.toJSONString(this);
        }
    }
}
